package km.TestJson;

import java.util.*;

import org.json.*;

import android.app.*;
import android.os.*;
import android.util.*;

public class TestJson extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String data ="{\"fname1\" : \"aaaa\",\"fname2\" : \"kkkk\"}";
//        String data ="[{\"fname\" : \"aaaa\"},{\"fname\" : \"kkkk\"}]";
//        String data ="[\"fname\" ,\"gggg\"]";
        String str ="";
        JSONArray jArray=null;
        JSONObject obj=null;
        try{
        	
//        	jArray = (JSONArray) new JSONTokener(data).nextValue();
//            jArray = obj.getJSONArray("fname");
//            for (int i = 0; i < jArray.length(); i++) {
//            	Log.e("", "fname"+i+">>"+jArray.get(i));
//			}
//            	Log.e("", "fname>>"+str);
        	ArrayList<String> fname = new ArrayList<String>();
        	ArrayList<String> fpath = new ArrayList<String>();
        	fname.add("ff1");
        	fname.add("ff2");
        	fname.add("ff3");
        	fpath.add("path1");
        	fpath.add("path2");
        	fpath.add("path3");
//        	String data =putJSONArray(fname,fpath).toString();
        	//String data =putJSONobj(fname,fpath).toString();
//        	getJSONArray(data);
        	getJSONobj(data);
        }catch (Exception e) {
        	e.printStackTrace();
		}
    }
    /**
     * JSON 객체에서 key 가져오기
     * @param key
     * @param data
     * @return
     */
    public ArrayList<String> getJSONobj(String data){
    	  JSONObject obj=null;
    	  ArrayList<String> keys =new ArrayList<String>();
    	  try{
          	obj = (JSONObject) new JSONTokener(data).nextValue();
//          	result = obj.keys()
          	for (Iterator iterator = obj.keys(); iterator.hasNext();) {
				String key = (String) iterator.next();
				keys.add(key);
				Log.e("", key+">>>>");
			}            	
    	  }catch (Exception e) {
    		  e.printStackTrace();
		  }
    	  return keys;
    }
    /**
     * JSON 객체에서 데이터가져오기
     * @param key
     * @param data
     * @return
     */
    public String getJSONobj(String key,String data){
    	  JSONObject obj=null;
    	  String result ="";
    	  try{
          	obj = (JSONObject) new JSONTokener(data).nextValue();
          	result = obj.getString(key);
            	Log.e("", key+">>>>"+result);
    	  }catch (Exception e) {
    		  e.printStackTrace();
		  }
    	  return result;
    }
    
    /**
     * JSON Array 에서 데이터 가져오기
     * @param data
     * @return
     */
    public ArrayList<String> getJSONArray(String data){
    	JSONArray jArray=null;
//    	  JSONObject obj=null;
    	  ArrayList<String> path =new ArrayList<String>();
    	  try{
          	jArray = (JSONArray) new JSONTokener(data).nextValue();
            for (int i = 0; i < jArray.length(); i++) {
//            	obj=(JSONObject)jArray.get(i);
//            	path.add(obj.getString("filePath"));
            	path.add(jArray.get(i).toString());
            	Log.e("", "jArray"+i+">>>>"+jArray.get(i).toString());
//            	Log.e("", "obj"+i+">>>>"+obj);
//            	Log.e("", "filePath"+i+">>>>"+obj.getString("filePath"));
			}
    	  }catch (Exception e) {
    		  e.printStackTrace();
		  }
    	  return path;
    }
    /**
     * JSON Array 에 데이터 넣기
     * @param fname
     * @param fpath
     * @return
     */
    public String putJSONArray(ArrayList<String> fname ,ArrayList<String> fpath){
    	JSONArray array=new JSONArray();
    	  JSONObject obj=new JSONObject();
    	  try{
    	  for(int i=0;i<fname.size();i++){
        	  obj.put("fileName",fname.get(i));
        	  obj.put("filePath",fpath.get(i));
        	  array.put(obj);
    	  }

    	  Log.e("","array>>>>"+ array);
    	  }catch (Exception e) {
    		  e.printStackTrace();
		  }
    	  return array.toString();
    }
	/**
	 *  JSON 객체에 데이터 넣기
	 * @param key
	 * @param data
	 * @return
	 */
    public static String putJSONobj(ArrayList<String> key,ArrayList<String> value){
    	JSONObject obj=new JSONObject();
    	  try{
        	  for(int i=0;i<key.size();i++){
        		  obj.put(key.get(i),value.get(i));        		  
        	  }
            	Log.e("", "obj>>>>"+obj);
    	  }catch (Exception e) {
    		  e.printStackTrace();
		  }
    	  return obj.toString();
    }
    
}